namespace Voodoo.Sauce.Internal.Ads.MopubMediation
{
	public class MopubBanner : BannerAd
	{
		public MopubBanner(string adUnit)
			: base("Mopub", adUnit)
		{
			MoPubWrapper.LoadBannerPluginsForAdUnits(adUnit);
			RegisterEvents();
		}

		public override void Show()
		{
			base.Show();
			MoPubAndroid.CreateBanner(_adUnit, MoPubBase.AdPosition.BottomCenter);
			MoPubAndroid.ShowBanner(_adUnit, shouldShow: true);
		}

		public override void Hide()
		{
			if (_isShowing)
			{
				MoPubAndroid.ShowBanner(_adUnit, shouldShow: false);
			}
			base.Hide();
		}

		public override void Destroy()
		{
			if (_isShowing)
			{
				MoPubAndroid.DestroyBanner(_adUnit);
			}
			base.Destroy();
		}

		protected override void RegisterEvents()
		{
			base.RegisterEvents();
			MoPubManager.OnAdLoadedEvent += OnMoPubAdLoaded;
			MoPubManager.OnAdFailedEvent += OnMoPubAdFailed;
			MoPubManager.OnAdClickedEvent += OnMoPubAdClicked;
		}

		protected override void UnregisterEvents()
		{
			base.UnregisterEvents();
			MoPubManager.OnAdLoadedEvent -= OnMoPubAdLoaded;
			MoPubManager.OnAdFailedEvent -= OnMoPubAdFailed;
			MoPubManager.OnAdClickedEvent -= OnMoPubAdClicked;
		}

		private void OnMoPubAdFailed(string adUnit, string errorMsg)
		{
			if (!(adUnit != _adUnit))
			{
				OnAdFailedCallback(errorMsg);
			}
		}

		private void OnMoPubAdLoaded(string adUnit, float height)
		{
			if (!(adUnit != _adUnit))
			{
				OnAdLoadedCallback(height);
			}
		}

		private void OnMoPubAdClicked(string adUnit)
		{
			if (!(adUnit != _adUnit))
			{
				OnAdClickedCallback();
			}
		}
	}
}
